<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/2
 * Time: 10:46
 */

namespace app\admin\controller;


use app\service\ResourceService;
use think\Controller;
use think\facade\Request;
use think\facade\Session;

class Common extends Controller
{
    protected $data_post;
    protected $data_get;
    protected $data_request;
    /**
     * 构造方法
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function __construct()
    {
        parent::__construct();
        $this->data_post = input('post.');
        $this->data_get = input('get.');
        $this->data_request = input();

        // 登录校验
        $this->IsLogin();
    }


    protected function IsLogin()
    {
        $uid = Session('uid');
        if(empty($uid)) {
            $this->redirect("/admin/login/index.html");
        } else {

            $this->assign('account', Session('account'));
            return true;
        }
    }
    /**
     * 上传文件方法
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function Doupload(){
             $filename=Request::param('field');
             $mark=Request::param('mark');
             if(empty($mark)) $mark='img';
             $oldsrc=Request::param('oldSrc');
             $src=ResourceService::uploadFile($mark,$oldsrc,$filename);
              return json($src);
    }

    /**
     * 处理多余图片
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function DelRemainder($in_use,$in_all){

        if(strlen($in_use[0])<1)
        {
            unset($in_use[0]);
        }
        if(strlen($in_all[0])<1)
        {
            unset($in_all[0]);
        }

        foreach ($in_all as $k=>$v){

            if(in_array($v,$in_use))
            {
                unset($in_all[$k]);
            }
        }

        if(count($in_all)>0)
        {
            foreach ($in_all as $v){
                ## 从缓存中删除
                ResourceService::delCacheItem($v)  ;
                ## 从路径中删除
                ResourceService::delFile($v);
            }
            return json(DataReturn('删除成功',0));
        }else{
            //throw new \BaseException(array("code"=>403,"msg"=>"数据格式错误","errorcode"=>00000,"status"=>false));
            return json(DataReturn('未发现多余图片',0));
        }

    }



}